﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSTreeSAX.fuctions
{
    class SortMbr : ISortable
    {
        public int dimension; 	// the dimension of the MBR according to which the sorting is done
        public char[] mbr; 	// the data of the MBR
        public char[] center;	// the MBR's centroid
        public int index;		// the MBR's index inside an array of mbrs
        public int length;

       public SortMbr(int length)
        {
            this.length = length;
        }
        private float computeErg(ISortable s, int sortCriterion)
        {
           
            float erg = (float)0.0;

            switch (sortCriterion)
            {
                case Constants.SORT_LOWER_MBR:
                    erg = Utilities.SaxDistance(this.mbr[2 * dimension], ((SortMbr)s).mbr[2 * dimension], length);
                    break;
                case Constants.SORT_UPPER_MBR:

                    erg = Utilities.SaxDistance(this.mbr[2 * dimension + 1], ((SortMbr)s).mbr[2 * dimension + 1], length);
                    break;
                case Constants.SORT_CENTER_MBR:

                    float d, e1, e2;
                    e1 = e2 = (float)0.0;
                    for (int i = 0; i < dimension; i++)
                    {

                        d = Utilities.SaxDistance(Convert.ToChar(Math.Abs((int)this.mbr[2 * i] - (int)this.mbr[2 * i + 1]) / 2 + 33), this.center[i], length);
                        e1 += d * d;

                        d = Utilities.SaxDistance(Convert.ToChar(Math.Abs((int)((SortMbr)s).mbr[2 * i] - (int)((SortMbr)s).mbr[2 * i + 1]) / 2 + 33), ((SortMbr)s).center[i], length);
                        e2 += d * d;
                    }

                    erg = e1 - e2;
                    break;
            }
            return erg;
        }

        public Boolean lessThan(ISortable s, int sortCriterion)
        {
            float erg = computeErg(s, sortCriterion);
            if (erg < (float)0.0)
                return true;
            return false;
        }

        public Boolean greaterThan(ISortable s, int sortCriterion)
        {
            float erg = computeErg(s, sortCriterion);
            if (erg > (float)0.0)
                return true;
            return false;
        }
    }
}
